package com.kk.miaosha.controller;

import com.kk.miaosha.entity.ItemKill;
import com.kk.miaosha.entity.ItemKillSuccess;
import com.kk.miaosha.exception.R;
import com.kk.miaosha.service.ItemKillService;
import com.kk.miaosha.service.ItemKillSuccessService;
import com.kk.miaosha.utils.RedisKeyUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author : K k
 * @date : 21:20 2020/10/20
 */
@RestController
@Api(value = "PayController",description = "支付服务的接口")
@RequestMapping("pay")
public class PayController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ItemKillSuccessService iksService;
    @Autowired
    private ItemKillService itemKillService;

    //查询订单状态
    @PostMapping("/getOrder/{uuid}/{userId}")
    public R getOrderStatus(@PathVariable("uuid")String uuid,@PathVariable("userId")Integer userId){
        //获取用户订单  userId和uuid唯一确认一个订单消息
        HashMap<String,Object> map= (HashMap<String, Object>) redisTemplate.opsForHash().get(RedisKeyUtil.getKillSuccessKey(userId), uuid);
        if (map==null){
            return R.error().message("查无订单！");
        }
        return R.ok().data(map);
    }

    //查询所有订单状态
    @PostMapping("/getAllOrder/{userId}")
    public R getAllOrder(@PathVariable("userId")Integer userId){
        List<HashMap<String,Object>> orders = redisTemplate.opsForHash().values(RedisKeyUtil.getKillSuccessKey(userId));
        if (orders==null || orders.size()==0){
            return R.error("暂无订单");
        }
        return R.ok().data("orders",orders);
    }

    //支付
    @PostMapping("/payOrder/{uuid}/{userId}")
    public R payOrder(@PathVariable("uuid")String uuid,@PathVariable("userId")Integer userId){
        HashMap<String,Object> map= (HashMap<String, Object>) redisTemplate.opsForHash().get(RedisKeyUtil.getKillSuccessKey(userId), uuid);
        if (map!=null) {
            redisTemplate.opsForHash().delete(RedisKeyUtil.getKillSuccessKey(userId), uuid);
            //写入数据库
            System.out.println("订单写入数据库");
            //扣除真实库存
            iksService.updateStock((Integer) map.get("killId"),(Integer) map.get("itemId"),(Integer) map.get("userId"));
            return R.ok().message("支付成功！");
        }else {
            return R.error().message("订单失效！");
        }
    }

    //取消订单
    @PostMapping("cancel/{uuid}/{userId}")
    public R deleteOrder(@PathVariable("uuid")String uuid,@PathVariable("userId")Integer userId){
        Boolean flag = redisTemplate.opsForHash().hasKey(RedisKeyUtil.getKillSuccessKey(userId), uuid);
        if (flag) {
            redisTemplate.opsForHash().delete(RedisKeyUtil.getKillSuccessKey(userId), uuid);
            //Integer itemId = (Integer) map.get("itemId");
            //Integer killId = (Integer) map.get("killId");
            //redisTemplate.opsForList().rightPush(RedisKeyUtil.getKillStockKey(itemId,killId),uuid);

            //TODO 退款等服务
            return R.ok().message("取消成功！");
        }else {
            return R.error().message("订单已失效！");
        }
    }
}
